package xiaolicarrental.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xiaolicarrental.model.Car;
import xiaolicarrental.model.Result;
import xiaolicarrental.service.CarService;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/car")
public class CarController {
    @Autowired
    private CarService carService;

    @GetMapping
    public Result list() {
        log.info("查询全部车辆");
        List<Car> cars =carService.list();
        return Result.success(cars);
    }
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据ID查询车辆");
        Car car = carService.getById(id);
        return Result.success(car);

    }
    @PostMapping
    public Result add(@RequestBody Car car) {
        log.info("添加车辆");
        carService.add(car);
        return Result.success();
    }
    @PutMapping("/{id}")
    //根据Id删除车辆
    public Result delete(@PathVariable Integer id){
        carService.deleteById(id);
        System.out.println("删除车辆");
        return Result.success();
    }


}
